package com.cn.web.controller.admin;


import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.bc.Button;
import com.cn.config.config.Msg;
import com.cn.service.server.admin.AdminButtonServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class AdminButtonController {
    private final AdminButtonServer adminButtonServer;
    @Autowired
    public AdminButtonController(AdminButtonServer adminButtonServer) {
        this.adminButtonServer = adminButtonServer;
    }
    @RequestMapping("/select/Admin/Button")
    @ResponseBody
    public List<Button> selectAdminButton(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String buttonId = jsonObject.getString("buttonId");
        String buttonFunction = jsonObject.getString("buttonFunction");
        buttonId = (buttonId != null) ? buttonId : "";
        buttonFunction = (buttonFunction != null) ? buttonFunction : "";
        List<Button> buttons = adminButtonServer.selectAdminButton(buttonId, buttonFunction, limit, page);
        return buttons;
    }
    @RequestMapping("/insert/Admin/Button")
    @ResponseBody
    public Msg insertAdminButton(@RequestBody JSONObject jsonObject) {
        Msg msg=new Msg();
        String buttonName = jsonObject.getString("buttonName");
        String remark = jsonObject.getString("remark");
        String buttonFunction = jsonObject.getString("buttonFunction");
        Integer menuId = jsonObject.getInteger("menuId");
        String operationType = "无";

        int buttonId = adminButtonServer.getAutoId()+1;
        int i = adminButtonServer.insertAdminButton(buttonId,buttonName,remark,buttonFunction,menuId,operationType);
        if (i>0){
            msg.setCode(200);
            msg.setMessage("保存成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("保存失败");
        return msg;
    }
    @RequestMapping("/update/Admin/Button")
    @ResponseBody
    public Msg updateAdminButton(@RequestBody JSONObject jsonObject) {
        Msg msg=new Msg();
        int buttonId = jsonObject.getInteger("buttonId");
        String buttonName = jsonObject.getString("buttonName");
        String remark = jsonObject.getString("remark");
        String buttonFunction = jsonObject.getString("buttonFunction");
        Integer menuId = jsonObject.getInteger("menuId");
        String operationType = "无";
        int i = adminButtonServer.updateAdminButton(buttonId,buttonName,remark,buttonFunction,menuId,operationType);
        if (i>0){
            msg.setCode(200);
            msg.setMessage("修改成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("修改失败");
        return msg;
    }
    @RequestMapping("/delete/Admin/Button")
    @ResponseBody
    public Msg deleteAdminButton(@RequestBody JSONObject jsonObject) {
        Integer buttonId = jsonObject.getInteger("buttonId");
        Msg msg=new Msg();
        int i = adminButtonServer.deleteAdminButton(buttonId);
        if (i>0){
            msg.setCode(200);
            msg.setMessage("删除成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("删除失败");
        return msg;
    }
}
